#include <cstdio>
#include <queue>

using namespace std;

struct Node
{
    int x, y, t;
    Node( int _x, int _y, int _t )
        : x( _x ), y( _y ), t( _t ) { }
    Node( ) { }
};

int n, m, x1, y1, x2, y2;
char map[ 21 ][ 21 ];
bool visit[ 21 ][ 21 ][ 2 ];
queue<Node> q;

void init( )
{
    int i, j;
    for ( i = 0; i < n; i++ )
        scanf("%s", map[ i ]);
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < m; j++ )
        {
            visit[ i ][ j ][ 0 ] = visit[ i ][ j ][ 1 ] = false;
            if ( map[ i ][ j ] == 'S' )
            {
                x1 = i;
                y1 = j;
            }
            if ( map[ i ][ j ] == 'T' )
            {
                x2 = i;
                y2 = j;
            }
        }
    visit[ x1 ][ y1 ][ 0 ] = true;
}

void pushNode( int x, int y, int t, int f1, int f2 )
{
    if ( x < 0 || x >= n || y < 0 || y >= m )
        return ;
    if ( map[ x ][ y ] == '*' )
        return ;
    if ( visit[ x ][ y ][ t & 1 ] )
        return ;
    if ( map[ x ][ y ] == '|' )
    {
        if ( t & 1 )
        {
            if ( f1 == 0 )
                return ;
        }
        else
            if ( f2 == 0 )
                return ;
        pushNode( x + f1, y + f2, t, f1, f2 );
    }
    else if ( map[ x ][ y ] == '-' )
    {
        if ( t & 1 )
        {
            if ( f2 == 0 )
                return ;
        }
        else
            if ( f1 == 0 )
                return ;
        pushNode( x + f1, y + f2, t, f1, f2 );
    }
    else
    {
        visit[ x ][ y ][ t & 1 ] = true;
        q.push( Node( x, y, t ) );
    }
}

void work( )
{
    Node p;
    while ( !q.empty( ) )
        q.pop( );
    q.push( Node( x1, y1, 0 ) );
    while ( !q.empty( ) )
    {
        p = q.front( );
        q.pop( );
        if ( p.x == x2 && p.y == y2 )
            break;
        pushNode( p.x + 1, p.y, p.t + 1, 1, 0 );
        pushNode( p.x - 1, p.y, p.t + 1, -1, 0 );
        pushNode( p.x, p.y + 1, p.t + 1, 0, 1 );
        pushNode( p.x, p.y - 1, p.t + 1, 0, -1 );
        pushNode( p.x, p.y, p.t + 1, 0, 0 );
    }
    printf("%d\n", p.t);
}

int main( )
{
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        init( );
        work( );
    }
    return 0;
}

